﻿using System;
using System.Text;
using OS.Common.Extention;

namespace OS.Common.Utils
{
    public static class NumberUtil
    {
        private static readonly long TimeStartTicks;

        private static readonly Random Random;

        private static readonly NumberGenerate Generate;

        public static string RandomNumber(int length = 4)
        {
            var stringBuilder = new StringBuilder();
            for (var i = 0; i < length; i++)
            {
                stringBuilder.Append(Random.Next(0, 9));
            }

            return stringBuilder.ToString();
        }

        public static long TimeMilliNumber()
        {
            return (DateTime.UtcNow.Ticks - TimeStartTicks) / 10000;
        }

        public static long SubTimeNumber(long mainNumber)
        {
            var number = mainNumber % 10000;
            return string.Concat(TimeMilliNumber(), number).ToInt64(0);
        }

        static NumberUtil()
        {
            DateTime dateTime = new DateTime(2018, 1, 1);
            dateTime = dateTime.ToUniversalTime();
            TimeStartTicks = dateTime.Ticks;
            dateTime = DateTime.Now;
            Random = new Random(dateTime.Millisecond);
            Generate = new NumberGenerate(0L);
        }
    }
}